#ifndef CLIENTHANDLER_H
#define CLIENTHANDLER_H

#include <QThread>
#include <QTcpSocket>
#include <QDataStream>
#include <dbmanager.h>

class ServerThread : public QThread
{
    Q_OBJECT

public:
    explicit ServerThread(QTcpSocket *socket, QObject *parent = nullptr);
    ~ServerThread();

protected:
    void run() override;  // 重写QThread的run方法

private slots:
    void onReadyRead();
    void onDisconnected();

private:
    QTcpSocket *clientSocket;
    DbManager dbManager;  // 数据库管理实例

    void handleChatHistory(QTcpSocket *clientSocket,int user_one_id, int user_two_id);
    void handleInformation(QTcpSocket *clientSocket,int user_id);
    void handleFriendList(QTcpSocket *clientSocket,int user_id);

    void handleInsertChatHistory(QTcpSocket *clientSocket,int user_id_1,int user_id_2,ChatHistory history);

    void handleUpdateUserInformation(QTcpSocket *clientSocket, User user);

    // 处理用户插入
    void handleInsertUserInformation(QTcpSocket *clientSocket, User user);

    void handleInsertFriendRelation(QTcpSocket * clientSocket, int user_one_id,int user_two_id);

    void handleDeleteFriendRelation(QTcpSocket *clientSocket,int user_one_id,int user_two_id);

    void handleError();
};

#endif // CLIENTHANDLER_H
